package org.wevil.controller.sys;

import org.wevil.aspect.LogAnnotation;
import org.wevil.entity.sys.SysUser;
import org.wevil.enumu.ResultEnum;
import org.wevil.params.login.LoginNameParam;
import org.wevil.params.login.LoginTelParam;
import org.wevil.params.update.WxUserUP;
import org.wevil.repo.sys.SysUserRepo;
import org.wevil.service.sys.UserService;
import org.wevil.service.sys.WxService;
import org.wevil.util.JwtUtil;
import org.wevil.vo.Result;
import org.wevil.vo.wx.WxVo;



import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/** 登录接口 */
@Validated
@RestController
@RequestMapping("login")
public class LoginController {

    @Resource
    private SysUserRepo sysUserRepo;
    @Resource
    private WxService wxService;
    @Resource
    private UserService userService;

    /** 手机号登录 */
    @LogAnnotation
    @PostMapping("/tel")
    public Result<String> loginTel(
            @RequestBody @Validated LoginTelParam param
    ) {
        String tel = param.getTel();
        String password = param.getPassword();
        SysUser sysUser = sysUserRepo.findByTel(tel);
        if (sysUser != null) {
            String sysPassword = sysUser.getPassword();
            if (password.equals(sysPassword)) {
                String token = JwtUtil.getToken(sysUser);
                return Result.success(token);
            } else {
                return Result.info(ResultEnum.BLANK, "密码错误");
            }
        } else {
            return Result.info(ResultEnum.BLANK, "帐号不存在");
        }
    }

    /** 用户名登录 */
    @LogAnnotation
    @PostMapping("/name")
    public Result<String> login(
            @RequestBody @Validated LoginNameParam loginParam
    ) {
        String username = loginParam.getUsername();
        String password = loginParam.getPassword();
        SysUser sysUser = sysUserRepo.findByUsername(username);
        if (sysUser != null) {
            String sysPassword = sysUser.getPassword();
            if (password.equals(sysPassword)) {
                String token = JwtUtil.getToken(sysUser);
                return Result.success(token);
            } else {
                return Result.info(ResultEnum.BLANK, "密码错误");
            }
        } else {
            return Result.info(ResultEnum.BLANK, "用户名不存在");
        }
    }

    /**
     * 做小程序登录 只需要保存这个openid就可以 这个可以算是微信登录的唯一标识 用户信息那些 前台可以自己获取 根据前台获取到的存入数据库即可
     * 获取openid，只有获取到openid，微信才算你授权了
     */
    @PostMapping("/wx")
    public Result<WxVo> getTokenWx(
             @RequestParam String code) {
        WxVo result = wxService.getOpenId(code);
        return Result.success(result);
    }

    /** 微信用户数据推送，返回token */
    @PutMapping("/login/wx/push")
    public Result<String> push(
            @RequestBody @Validated WxUserUP param
    ) {
        SysUser sysUser = userService.updateWxUser(param);
        if(sysUser == null){
            return Result.info(ResultEnum.BLANK);
        }
        String token = JwtUtil.getToken(sysUser);
        return Result.success(token);
    }
}
